#include <stdio.h>
#include <malloc.h>

#define MAX_DEGREE 10000

//? singly linked list for polynomial ADT
typedef struct node *node_ptr;

struct node {
    int coefficient;
    int exponent;
    node_ptr next;
};

typedef node_ptr POLYNOMIAL;


//? initialize polynomial, poly is the sentinel
void zero_polynomial(POLYNOMIAL poly) {
    unsigned int i;
    for (i = 0; i <= MAX_DEGREE; i++) {
        POLYNOMIAL tmp_cell = (POLYNOMIAL) malloc(sizeof(struct node));
        tmp_cell->coefficient = 0;
        tmp_cell->exponent = 0;
        tmp_cell->next = poly->next;
        poly->next = tmp_cell;
    }
}

int max(int h1, int h2) {
    if (h1 > h2) {
        return h1;
    } else {
        return h2;
    }
}

//? because linked POLYNOMIAL exponents is decrease, so the high_power is the first cell.
void add_polynomial(POLYNOMIAL poly1, POLYNOMIAL poly2, POLYNOMIAL poly_sum) {
    int i;
    zero_polynomial(poly_sum);
    POLYNOMIAL tmp_poly1, tmp_poly2, tmp_poly_sum;
    tmp_poly1 = poly1->next;
    tmp_poly2 = poly2->next;
    tmp_poly_sum = poly_sum->next;
    tmp_poly_sum->exponent = max(tmp_poly1->exponent, tmp_poly2->exponent);
    for (i = tmp_poly_sum->exponent; i >= 0; i--) {
        tmp_poly_sum->coefficient = tmp_poly1->coefficient + tmp_poly2->coefficient;
        tmp_poly_sum = tmp_poly_sum->next;
        tmp_poly1 = tmp_poly1->next;
        tmp_poly2 = tmp_poly2->next;
    }
}

void error(char *str) {
}

//? because linked POLYNOMIAL exponents is decrease, so the high_power is the first cell.
void mult_polynomial(POLYNOMIAL poly1, POLYNOMIAL poly2, POLYNOMIAL poly_prod) {
    unsigned int i, j;
    zero_polynomial(poly_prod);
    POLYNOMIAL tmp_poly1, tmp_poly2, tmp_poly_prod;
    tmp_poly1 = poly1->next;
    tmp_poly2 = poly2->next;
    tmp_poly_prod = poly_prod->next;
    tmp_poly_prod->exponent = tmp_poly1->exponent + tmp_poly2->exponent;
    if (tmp_poly_prod->exponent > MAX_DEGREE) {
        error("Exceeded list max length");
    } else {
        for (i = 0; i <= tmp_poly1->exponent; i++) {
            for (j = 0; j <= tmp_poly2->exponent; j++) {
                tmp_poly_prod->coefficient += tmp_poly1->coefficient * tmp_poly2->coefficient;
                tmp_poly2 = tmp_poly2->next;
            }
            tmp_poly1 = tmp_poly1->next;
            tmp_poly_prod = tmp_poly_prod->next;
            tmp_poly2 = poly_prod->next;
        }
    }
}

int main() {
    printf("Hello, World!\n");
    return 0;
}
